package com.example.bootcurriculumproject.testController;

import com.example.bootcurriculumproject.entity.Cart;
import com.example.bootcurriculumproject.mapper.CartMapper;
import com.example.bootcurriculumproject.service.CartService;
import com.example.bootcurriculumproject.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@Slf4j
public class testCartController {
    @MockBean
    private CartService cartService;
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private RedisUtil redisUtil;
    @MockBean
    private CartMapper cartMapper;
    private static final String URL = "/cart";
    @Test
    public void testAdd() throws Exception {
        String body = "{\"cartnum\": \"aaaaaa\",\"uid\": \"bbbb\",\"buynumber\": 0,\"goodsno\": \"测试\",\"addressnum\": 23355}";
        Cart cart = new Cart("112233","aadddd",33,"33ddccc",3355);
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        //Mockito.when(cartService.getOne(any())).thenReturn(cart);
        Mockito.when(cartService.cartAdd(any())).thenReturn(true);
        //Mockito.doNothing().when(cartService).save(any());
        String result = this.mockMvc.perform(MockMvcRequestBuilders.post("/cart/add")
                        .accept(MediaType.APPLICATION_JSON)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testDeleteOne() throws Exception {
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(cartService.cartRemove(anyString())).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.delete("/cart/deleteOne/111")
                        .accept(MediaType.APPLICATION_JSON)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }

    @Test
    public void testClean() throws Exception {
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(cartService.cartRemove(anyString())).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.delete("/cart/deleteOne/111,222,333")
                        .accept(MediaType.APPLICATION_JSON)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }

}
